package com.benjaminwan.read.free.config

import com.benjaminwan.read.free.base.viewmodel.state
import com.benjaminwan.read.free.model.*
import com.benjaminwan.read.free.model.enums.ItemType
import com.benjaminwan.read.free.ui.main.MainViewModel
import com.benjaminwan.read.free.utils.*
import com.xuexiang.xui.widget.textview.supertextview.SuperTextView

object ScriptConfig {
    fun getScriptConfig(mainVM: MainViewModel): List<ItemConfig> {
        return listOfItemConfig {
            itemConfig {
                type = ItemType.Text
                titleStr = "备注"
                rightTextFunc = { mainVM.state.script.memo }
                click = { view ->
                    (view as SuperTextView).context.showTextInputDialog(
                        title, "请输入$title", rightText, "", 0, -1, null,"默认",
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val script = mainVM.state.script
                        mainVM.setScript(script.copy(memo = input))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "脚本循环"
                rightTextFunc = { "${mainVM.state.script.globalConfig.loopRange}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val script = mainVM.state.script
                        if (input.isEmpty()) {
                            val global = script.globalConfig.copy(loopRange = (1..1))
                            mainVM.setScript(script.copy(globalConfig = global))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            val global = script.globalConfig.copy(loopRange = (start..end))
                            mainVM.setScript(script.copy(globalConfig = global))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "屏幕x"
                rightTextFunc = { "${mainVM.state.script.globalConfig.screenX}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, -1, getRegexFilter(numRegex),
                        { dialog, which ->
                            val default = "720"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val script = mainVM.state.script
                        val screenX = input.toIntOrNull() ?: 720
                        val global = script.globalConfig.copy(screenX = screenX)
                        mainVM.setScript(script.copy(globalConfig = global))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "屏幕y"
                rightTextFunc = { "${mainVM.state.script.globalConfig.screenY}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 1, -1, getRegexFilter(numRegex),
                        { dialog, which ->
                            val default = "1440"
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val script = mainVM.state.script
                        val screenY = input.toIntOrNull() ?: 1440
                        val global = script.globalConfig.copy(screenY = screenY)
                        mainVM.setScript(script.copy(globalConfig = global))
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "默认延时"
                rightTextFunc = { "${mainVM.state.script.globalConfig.delayRange}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val script = mainVM.state.script
                        if (input.isEmpty()) {
                            val global = script.globalConfig.copy(delayRange = (1000L..2000))
                            mainVM.setScript(script.copy(globalConfig = global))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toLongOrNull()
                            val end = range[1].toLongOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            val global = script.globalConfig.copy(delayRange = (start..end))
                            mainVM.setScript(script.copy(globalConfig = global))
                        }
                        dialog.dismiss()
                    }
                }
            }
            itemConfig {
                type = ItemType.Text
                titleStr = "默认鼠标步进"
                rightTextFunc = { "${mainVM.state.script.globalConfig.stepRange}" }
                click = { view ->
                    (view as SuperTextView).showRightTextInputDialog(
                        title, 0, -1, getRegexFilter(numRegex, "[.]+"),
                        { dialog, which ->
                            val default = ""
                            dialog.inputEditText?.setText(default)
                            dialog.inputEditText?.setSelection(default.length)
                        }) { dialog, which ->
                        val input = dialog.inputEditText?.text.toString()
                        val script = mainVM.state.script
                        if (input.isEmpty()) {
                            val global = script.globalConfig.copy(stepRange = (3..8))
                            mainVM.setScript(script.copy(globalConfig = global))
                        } else if (!input.contains("..")) {
                            dialog.view.shakeReverse()
                            view.context.showXToastError("输入格式错误")
                            return@showRightTextInputDialog
                        } else {
                            val range = input.split("..")
                            if (range.isEmpty() || range.size != 2) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误")
                                return@showRightTextInputDialog
                            }
                            val start = range[0].toIntOrNull()
                            val end = range[1].toIntOrNull()
                            if (start == null || end == null) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误:数字转换错误")
                                return@showRightTextInputDialog
                            }
                            if (start > end) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,起始($start)>结束($end)")
                                return@showRightTextInputDialog
                            }
                            if (start == 0 && end == 0) {
                                dialog.view.shakeReverse()
                                view.context.showXToastError("输入格式错误,不允许起始($start)=0和结束($end)=0")
                                return@showRightTextInputDialog
                            }
                            val global = script.globalConfig.copy(stepRange = (start..end))
                            mainVM.setScript(script.copy(globalConfig = global))
                        }
                        dialog.dismiss()
                    }
                }
            }
        }
    }
}